package com.songcha.library_common.util

import android.app.ActivityManager
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import com.songcha.library_common.helper.SharedPreferenceHelper
import java.util.UUID

class DeviceUtil {
    companion object{
        @JvmStatic
        fun getDeviceName():String{
            try{
                val deviceName = Build.BRAND + " " + Build.MODEL + " " + Build.VERSION.RELEASE
                return deviceName
            }catch (e:Exception){
                e.printStackTrace()
            }
            return "unknow"
        }

        @JvmStatic
        fun getAndroidId(context: Context):String{
            var androidId = Settings.System.getString(
                context.contentResolver, Settings.Secure.ANDROID_ID
            )
            if(androidId.isNullOrBlank()){
                var tmp = SharedPreferenceHelper.getString("device","android_id")
                if(tmp.isEmpty()){
                    tmp = UUID.randomUUID().toString()
                    SharedPreferenceHelper.setString("device","android_id",tmp)
                }
                androidId = tmp
            }
            return androidId
        }

        @JvmStatic
        fun getAvailableMemory(context:Context):Long{
            val activityManager = context.getSystemService(AppCompatActivity.ACTIVITY_SERVICE) as ActivityManager
            val memoryInfo = ActivityManager.MemoryInfo()
            activityManager.getMemoryInfo(memoryInfo)
            //val totalMemory = memoryInfo.totalMem / (1024 * 1024)
            val availableMemory = memoryInfo.availMem / (1024 * 1024)
            return availableMemory
        }
    }
}